package com.bodacn.keypoint;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;


public class CutQueryActivity extends Activity implements View.OnClickListener {
    private String TAG=this.getClass().getName();
    private final int MSG_Refresh=1001;
    private final int MSG_CutData=9001;

    Button btn_Return,btn_Refresh;
    String curLoginUserNo,curLoginUser,curServiceUrl="",curServiceDir="",curDepartNo,curDepart,CPUID;
    String curOrderNo,curStyleNo,curStyleSort,curProduceMasterID;
    String Size1,Size2,Size3,Size4,Size5,Size6,Size7,Size8,Size9,Size10,Size11,Size12;
    ListView detailList;

    //置值
    int SpaceSec=15;

    JSONArray detailJson;

    public  String TransValue(String params)
    {
        if (params.toUpperCase().equals("NULL")||params.equals("0")||params.equals("0.0"))
            return "";
        else return params;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cutquery);
        //获取登陆参数
        Bundle paramBundle = this.getIntent().getExtras();
        curLoginUserNo=paramBundle.getString("LoginUserNo");
        curLoginUser=paramBundle.getString("LoginUser");
        curDepartNo=paramBundle.getString("CurDepartNO");
        curDepart=paramBundle.getString("CurDepart");
        curServiceUrl=paramBundle.getString("ServiceUrl");
        curServiceDir=paramBundle.getString("ServiceDir");
        SpaceSec=paramBundle.getInt("SpaceSec");
        CPUID=paramBundle.getString("CPUID");
        //界面信息
        curProduceMasterID=paramBundle.getString("ProduceMasterID");
        curOrderNo=paramBundle.getString("OrderNo");
        curStyleNo=paramBundle.getString("StyleNo");
        curStyleSort=paramBundle.getString("StyleSort");
        Size1=paramBundle.getString("Size1");
        Size2=paramBundle.getString("Size2");
        Size3=paramBundle.getString("Size3");
        Size4=paramBundle.getString("Size4");
        Size5=paramBundle.getString("Size5");
        Size6=paramBundle.getString("Size6");
        Size7=paramBundle.getString("Size7");
        Size8=paramBundle.getString("Size8");
        Size9=paramBundle.getString("Size9");
        Size10=paramBundle.getString("Size10");
        Size11=paramBundle.getString("Size11");
        Size12=paramBundle.getString("Size12");
        System.out.println(paramBundle);
        //初始化界面
        btn_Refresh=findViewById(R.id.btn_Refresh);
        btn_Refresh.setOnClickListener(this);
        btn_Return=findViewById(R.id.btn_Return);
        btn_Return.setOnClickListener(this);
        detailList=findViewById(R.id.colorsizeList);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.btn_Refresh:
                GetCutData(curProduceMasterID);
                break;
            case R.id.btn_Return:
                finish();
                break;
            default:
                break;
        }
    }



    //获取图标数据;
    private void GetCutData(String paramProduceMasterID){
        try {
            detailJson=new JSONArray("[]");
            DetailListAdapter detailListAdapter=new DetailListAdapter(this);
            detailList.setAdapter(detailListAdapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println(TAG+"--------------------加载数据");
        //执行SQL过程
        String SqlComm="Exec KeyPoint_CutQuery @ProduceMasterID='" + paramProduceMasterID + "',@OperType='CutData'";
        //调用WebService过程并处理界面更新;
        ReadSQLData(SqlComm);
    }

    //订单表格适配器;
    public final class DetailListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public DetailListAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        @Override
        public int getCount() {
            return detailJson.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return detailJson.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) convertView = inflater.inflate(R.layout.activity_cutquery_listhead, null, false);
            int pullVatListWidth=parent.getWidth();
            convertView.setLayoutParams(new ListView.LayoutParams(pullVatListWidth,38));
            int color = getResources().getColor(R.color.text_black);
            String tmpValue;
            try {
                Log.d("msg","getView()a----------------------------------------------------->"+position);
                if (detailJson.optJSONObject(position).has("ConColor")==true) {
                    tmpValue = detailJson.optJSONObject(position).getString("ConColor").toString();
                    if (tmpValue.equals("1"))
                        color = getResources().getColor(R.color.text_blue);
                    if (tmpValue.equals("2"))
                        color = getResources().getColor(R.color.text_red);
                    if (tmpValue.equals("3"))
                        color = getResources().getColor(R.color.text_gray);
                    if (tmpValue.equals("0"))
                        color = getResources().getColor(R.color.text_black);
                }
                TextView tv =  convertView.findViewById(R.id.txt_item_RowType);// 显示文字
                if (detailJson.optJSONObject(position).has("RowType")==true)
                    tv.setText(detailJson.optJSONObject(position).get("RowType").toString());
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Color);// 显示文字
                if (detailJson.optJSONObject(position).has("Color")==true)
                    tv.setText(detailJson.optJSONObject(position).get("Color").toString());
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_ColorNo);// 显示文字
                if (detailJson.optJSONObject(position).has("ColorNo")==true)
                    tv.setText(detailJson.optJSONObject(position).get("ColorNo").toString());
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Pertain);// 显示文字
                if (detailJson.optJSONObject(position).has("Pertain")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Pertain").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size1);// 显示文字
                if (detailJson.optJSONObject(position).has("Size1")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size1").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size2);// 显示文字
                if (detailJson.optJSONObject(position).has("Size2")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size2").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size3);// 显示文字
                if (detailJson.optJSONObject(position).has("Size3")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size3").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size4);// 显示文字
                if (detailJson.optJSONObject(position).has("Size4")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size4").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size5);// 显示文字
                if (detailJson.optJSONObject(position).has("Size5")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size5").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size6);// 显示文字
                if (detailJson.optJSONObject(position).has("Size6")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size6").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size7);// 显示文字
                if (detailJson.optJSONObject(position).has("Size7")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size7").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size8);// 显示文字
                if (detailJson.optJSONObject(position).has("Size8")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size8").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size9);// 显示文字
                if (detailJson.optJSONObject(position).has("Size9")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size9").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size10);// 显示文字
                if (detailJson.optJSONObject(position).has("Size10")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size10").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size11);// 显示文字
                if (detailJson.optJSONObject(position).has("Size11")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size11").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Size12);// 显示文字
                if (detailJson.optJSONObject(position).has("Size12")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Size12").toString()));
                tv.setTextColor(color);
                tv =  convertView.findViewById(R.id.txt_item_Total);// 显示文字
                if (detailJson.optJSONObject(position).has("Total")==true)
                    tv.setText(TransValue(detailJson.optJSONObject(position).get("Total").toString()));
                tv.setTextColor(color);

            } catch (JSONException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }
            Log.d("msg","DetailListAdapter_position:"+position);
            return convertView;
        }
    }

    //通过消息来执行处理类程序,不更新UI则不一定需要通过这个;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // TODO Auto-generated method stub
            // 根据消息ID来判断要做的事情
            int msgId = msg.what;
            switch (msgId) {
                case MSG_Refresh:
                    GetCutData(curProduceMasterID);
                    break;
                case MSG_CutData:
                    //显示明细数据;
                    DetailListAdapter detailListAdapter=new DetailListAdapter(getApplicationContext());
                    detailList.setAdapter(detailListAdapter);
                    break;
                default:
                    break;
            }
        }
    };

    //直接连获取数据，与Handler配套使用;
    private void ReadSQLData(final String SQLComm) {
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try{
                    JSONArray jsonArray  = DBUtil.QuerySQL2JSON(SQLComm);
                    //往界面转数据;
                    if ((jsonArray!=null)&&(jsonArray.length()>0)) {
                        if (jsonArray.optJSONObject(0).has("OperType")==true) {
                            //呼叫信息及库存警告
                            if (jsonArray.optJSONObject(0).getString("OperType").equals("CutData")==true)
                            {
                                detailJson=jsonArray;
                                //显示预排单;
                                Message msg = new Message();
                                msg.what = MSG_CutData;
                                handler.removeMessages(msg.what);
                                handler.sendMessage(msg);
                            }
                        }



                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        };
        new Thread(run).start();

    }




    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onStart() {
        super.onStart();
        fillText();
        Message msg=new Message();
        msg.what=MSG_Refresh;
        handler.removeMessages(msg.what);
        handler.sendMessage(msg);
        System.out.println(TAG+"----onStart");
    }

    private void fillText() {
        ((TextView)findViewById(R.id.text_orderNo)).setText(curOrderNo);
        ((TextView)findViewById(R.id.text_styleNo)).setText(curStyleNo);
        ((TextView)findViewById(R.id.text_styleSort)).setText(curStyleSort);
        ((TextView)findViewById(R.id.txt_item_Size1)).setText(Size1);
        ((TextView)findViewById(R.id.txt_item_Size2)).setText(Size2);
        ((TextView)findViewById(R.id.txt_item_Size3)).setText(Size3);
        ((TextView)findViewById(R.id.txt_item_Size4)).setText(Size4);
        ((TextView)findViewById(R.id.txt_item_Size5)).setText(Size5);
        ((TextView)findViewById(R.id.txt_item_Size6)).setText(Size6);
        ((TextView)findViewById(R.id.txt_item_Size7)).setText(Size7);
        ((TextView)findViewById(R.id.txt_item_Size8)).setText(Size8);
        ((TextView)findViewById(R.id.txt_item_Size9)).setText(Size9);
        ((TextView)findViewById(R.id.txt_item_Size10)).setText(Size10);
        ((TextView)findViewById(R.id.txt_item_Size11)).setText(Size11);
        ((TextView)findViewById(R.id.txt_item_Size12)).setText(Size12);
    }

    @Override
    public void onPause() {
        super.onPause();
        System.out.println(TAG+"HouseActivity----onPause");
    }


}